package com.guigui.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.SysMenuDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.SystemUsers;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigui.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author Lenovo
 */
@CrossOrigin
@Controller
@RequestMapping("/design/file")
public class DesignFileController {
    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private NumberUtils numberUtils;

    /**
     * 产品档案添加
     */
    @RequestMapping("/add")
    @ResponseBody
    public String designFileAdd(DesignFile designFile) {
        // 重新设置各级分类
        String firstKindName = designFile.getFirstKindName();
        String[] firstAry = firstKindName.split("/");
        designFile.setFirstKindId(firstAry[0]);
        designFile.setFirstKindName(firstAry[1]);

        String secondKindName = designFile.getSecondKindName();
        String[] secondAry = secondKindName.split("/");
        designFile.setSecondKindId(secondAry[0]);
        designFile.setSecondKindName(secondAry[1]);

        String thirdKindName = designFile.getThirdKindName();
        String[] thirdAry = thirdKindName.split("/");
        designFile.setThirdKindId(thirdAry[0]);
        designFile.setThirdKindName(thirdAry[1]);

        // 设置标志位属性
        // 审核标志: 等待审核
        designFile.setCheckTag("S001-0");
        // 档案变更标志: 未变更
        designFile.setChangeTag("D002-0");
        // 价格变更标志：未变更
        designFile.setPriceChangeTag("J001-0");
        // 产品删除标志: 未删除
        designFile.setDeleteTag("C001-0");
        // 物料组成标志: 未设计
        designFile.setDesignModuleTag("W001-0");
        // 工序组成标志: 未设计
        designFile.setDesignProcedureTag("G001-0");
        // 库存分配标志: 未设计
        designFile.setDesignCellTag("K001-0");

        // 建档时间
        designFile.setRegisterTime(new Date());

        // 生成对应的产品档案编号
        String productNumber = numberUtils.getProductNumber(designFile.getFirstKindId(),
                designFile.getSecondKindId(),
                designFile.getThirdKindId());
        // 设置产品编号
        designFile.setProductId(productNumber);

        // 插入
        boolean flag = designFileService.save(designFile);
        if(flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 产品档案查询请求
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<DesignFile> designFileList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.page(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 产品档案登记复核查询
     */
    @RequestMapping("/designReview")
    @ResponseBody
    public PageUtils<DesignFile> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> recheckPage = this.designFileService.recheckPage(vo, pageNo, pageSize);
        return recheckPage;
    }

    /**
     * 产品设计 --产品复核通过
     * @param designFile
     * @return
     */
    @RequestMapping("designFileReviewPassed")
    @ResponseBody
    public String designFileReviewPassed(DesignFile designFile){
        designFile.setCheckTag("S001-1");
        boolean flag = designFileService.updateById(designFile);
        if (flag){
            return "correct";
        }
        return "error";
    }

    /**
     * 查询未制定过物料设计单的产品档案
     */
    @RequestMapping("/unMakeList")
    @ResponseBody
    public PageUtils<DesignFile> designFileUnMakeList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.unmakePage(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 查询未制定过工序组成设计单的产品档案
     */
    @RequestMapping("/unMakeList2")
    @ResponseBody
    public PageUtils<DesignFile> designFileUnMakeList2(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.unmakePage2(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 查询未制定安全库存配置单的产品档案
     */
    @RequestMapping("/unMakeList3/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<DesignFile> designFileUnMakeList3(@RequestBody DesignFileVo vo, @PathVariable int pageNo,@PathVariable int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.unmakePage3(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 根据产品档案id查询对应的产品档案信息
     */
    @RequestMapping("/designFileGetId/{id}")
    @ResponseBody
    public DesignFile designFileGetId(@PathVariable int id){
        DesignFile designFile=this.designFileService.getById(id);
        return designFile;
    }


    /**
     *查询制定物料设计单页面
     */
    @RequestMapping("/moduleFileList")
    @ResponseBody
    public PageUtils<DesignFile> moduleFileList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.moduleFilePage(vo, pageNo, pageSize);
        return page;
    }

    /**
     *查询制定物料设计单页面
     */
    @RequestMapping("/moduleFileList2")
    @ResponseBody
    public PageUtils<DesignFile> moduleFileList2(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = this.designFileService.moduleFilePage2(vo, pageNo, pageSize);
        return page;
    }

    /*//登录
    @RequestMapping("login")
    @ResponseBody
    public SystemUsers login(@RequestBody SystemUsers user){
        QueryWrapper<SystemUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("LOGIN_ID",user.getLoginId());
        wrapper.eq("PASSWORD",user.getPassword());
        return systemUsersService.getOne(wrapper);
    }*/
}
